#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
const int N = 1e4 + 10, INF = 2147483647;
typedef pair<int, int> pii;
vector<pii> edges[N];
int dist[N];
int n, m, s;
void bf()
{
    for (int i = 0; i <= n; i++)
    {
        dist[i] = INF;
    }
    dist[s] = 0;
    for (int i = 1; i < n; i++)
    {
        int flag = 0;
        for (int j = 1; j <= n; j++)
        {
            if (dist[j] == INF)
            {
                continue;
            }
            for (auto& e : edges[j])
            {
                int v = e.first;
                int w = e.second;
                if (dist[j] + w < dist[v])
                {
                    dist[v] = dist[j] + w;
                    flag = 1;
                }
            }
        }
        if (flag == 0)
        {
            break;
        }
    }
}
int main()
{
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edges[u].push_back({ v,w });
    }
    bf();
    for (int i = 1; i <= n; i++)
    {
        cout << dist[i] << " ";
    }
    return 0;
}